package com.qtyx.nms.util

import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*

/**
 * 时间工具
 */
object DateUtil {

    /**
     * 时间戳 转为 自定义日期格式
     * @param time  1541569323155
     * @param pattern yyyy-MM-dd HH:mm:ss
     * @return 2018-11-07 13:42:03
     */
    fun getDate2String(time: Long, pattern: String): String{
        val date = Date(time)
        val format = SimpleDateFormat(pattern, Locale.getDefault())
        format.timeZone = TimeZone.getTimeZone("GMT+08");  //设置时区，+08是北京时间
        return format.format(date)
    }


    /**
     * 字符串转换成时间戳
     * @param dateString 2018-11-07 13:42:03,
     * @param pattern yyyy-MM-dd HH:mm:ss
     * @return 1541569323000
     */
    fun getString2Date(dateString: String, pattern: String): Long {
        val dateFormat =SimpleDateFormat(pattern, Locale.getDefault())
        var date = Date()
        try {
            date = dateFormat.parse(dateString)
        } catch (e: ParseException) {
            e.printStackTrace()
        }
        return date.time
    }

    /**
     * 时间戳获取星期几
     * @param time 1541569323155
     */
    fun getWeek(time: Long): String {
        val cd = Calendar.getInstance()
        cd.time = Date(time)
        val year = cd[Calendar.YEAR] //获取年份
        val month = cd[Calendar.MONTH] //获取月份
        val day = cd[Calendar.DAY_OF_MONTH] //获取日期
        val week = cd[Calendar.DAY_OF_WEEK] //获取星期
        return when (week) {
            Calendar.SUNDAY -> "星期日"
            Calendar.MONDAY -> "星期一"
            Calendar.TUESDAY -> "星期二"
            Calendar.WEDNESDAY -> "星期三"
            Calendar.THURSDAY -> "星期四"
            Calendar.FRIDAY -> "星期五"
            else -> "星期六"
        }
    }

}